﻿using mars_server.csharp.AO;
using mars_server.csharp.AO.impl;
using mars_server.csharp.CO;
using mars_server.csharp.dao;
using mars_server.csharp.dao.impl;
using mars_server.csharp.enums;
using mars_server.csharp.model;
using mars_server.csharp.socket;
using mars_server.csharp.Util;
using mars_server.csharp.VO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace mars_server.csharp.controller
{
    class FriendController
    {
        private FriendController() { }
        private static readonly FriendController instance = new FriendController();
        internal static FriendController Instance
        {
            get
            {
                return instance;
            }
        }


        UserAO userAO = UserAOImpl.Instance;

        UserSocketAO userSocketAO = UserSocketAOImpl.Instance;

        RelationAO relationAO = RelationAOImpl.Instance;

        UserManager userManager = UserManagerImpl.Instance;


        /**
        * 搜寻在线用户
        * */
        public void findOnlineUser(MessageCO messageCO,UserSocket userSocket)
        {
            UserVO userVO = JsonUtil.j2o<UserVO>(messageCO.content);
            FriendsVO friendVO = new FriendsVO();
            friendVO.userVOs = userAO.queryUserVOsByUserVO(userVO);
            messageCO.content = friendVO;
            userSocketAO.sendMessageToUser(userSocket.userDO, messageCO);
        }

        /**
        * 获取好友列表
        * */
        public void getFriends(MessageCO messageCO,UserSocket userSocket)
        {
            List<UserDO> userDOs = relationAO.queryFriendDOsByUserId(userSocket.userDO.id);
            List<UserVO> userVOs = new List<UserVO>();
            foreach(UserDO userDO in userDOs)
            {
                UserVO userVO = new UserVO();
                userVO.id = userDO.id;
                userVO.nickName = userDO.nickName;
                userVO.sex = userDO.sex;
                userVO.headImg = userDO.headImg;
                userVO.userIntro = userDO.userIntro;
                userVO.isOnline =(int) IsOnlineEnum.online.NoOnline;
                if (userSocketAO.isUserOnlie(userDO.id))
                {
                    userVO.isOnline = (int)IsOnlineEnum.online.Online;
                }
                userVOs.Add(userVO);
            }
            FriendsVO friendsVO = new FriendsVO();
            friendsVO.userVOs = userVOs;
            messageCO.content = friendsVO;
            userSocketAO.sendMessageToSocket(userSocket,messageCO);
        }


        /**
         * 获取单个好友信息
         * */
        public void getFriendInfo(MessageCO messageCO,UserSocket userSocket)
        {
            UserDO userDO = new UserDO();
            UserVO userVO = JsonUtil.j2o<UserVO>(messageCO.content);
            if(userVO != null || userVO.id != 0)
            {
                userDO = userManager.selectByPrimaryKey(userVO.id);
            }
            messageCO.content = userDO;
            userSocketAO.sendMessageToUser(userSocket.userDO, messageCO);
        }

        /**
         * 处理添加好友请求
         * */
        public void dealAddFriendRequest(MessageCO messageCO , UserSocket userSocket)
        {
            FriendNotifyVO friendNotifyVO = JsonUtil.j2o<FriendNotifyVO>(messageCO.content);
            if(friendNotifyVO == null || friendNotifyVO.senderVO == null || friendNotifyVO.receiverVO == null)
            {
                return;
            }

            //注入请求双方信息
            UserDO senderDO1 = userManager.selectByPrimaryKey(friendNotifyVO.senderVO.id);
            friendNotifyVO.senderVO = DOVOutil.userDO2VO(senderDO1);
            UserDO receiverDO1 = userManager.selectByPrimaryKey(friendNotifyVO.receiverVO.id);
            friendNotifyVO.receiverVO = DOVOutil.userDO2VO(receiverDO1);
            messageCO.content = friendNotifyVO;

            if (friendNotifyVO.isAgree == (int)IsAgreeEnum.agree.Lanuch)//用户发起添加好友请求
            {
                UserDO recevierDO = new UserDO();
                recevierDO.id = friendNotifyVO.receiverVO.id;
                userSocketAO.sendMessageToUser(recevierDO, messageCO);
                Console.WriteLine("[info]|--【添加好友】--用户发起添加好友请求");
            }
            else if(friendNotifyVO.isAgree == (int)IsAgreeEnum.agree.DisAgree)
            {
                UserDO senderDO = new UserDO();
                senderDO.id = friendNotifyVO.senderVO.id;

                userSocketAO.sendMessageToUser(senderDO, messageCO);
                Console.WriteLine("[info]|--【拒绝添加】--用户拒绝添加好友请求");
            }
            else if(friendNotifyVO.isAgree == (int)IsAgreeEnum.agree.Agree)
            {
                long userId = friendNotifyVO.senderVO.id;
                long friendId = friendNotifyVO.receiverVO.id;
                relationAO.addFriend(userId, friendId);
                Console.WriteLine("[info]|--【同意添加】--用户同意添加好友请求");
                UserDO userDO = new UserDO();
                userDO.id = userId;
                userSocketAO.sendMessageToUser(userDO,messageCO);

            }
            else
            {
                return;
            }

        }

        /**
         * 处理删除好友请求
         * */
         public void dealDeleteFriendRquest(MessageCO messageCO, UserSocket userSocket)
        {
            UserVO userVO = JsonUtil.j2o<UserVO>(messageCO.content);
            if(userVO == null || userSocket.userDO == null)
            {
                return;
            }
            long deleteUserId = userVO.id;
            long requestUserId = userSocket.userDO.id;
            relationAO.deleteRelationByRelationDO(requestUserId, deleteUserId);

            UserVO deleteUserVO = new UserVO();
            UserDO deleteUserDO = userManager.selectByPrimaryKey(deleteUserId);
            deleteUserVO = DOVOutil.userDO2VO(deleteUserDO);

            messageCO.content = deleteUserVO;
            userSocketAO.sendMessageToUser(userSocket.userDO, messageCO);
        }
    }
}
